﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ortodoctor.Componentes
{
    public class PdfHandlerEvents : PdfPageEventHelper
    {
        iTextSharp.text.Font font = iTextSharp.text.FontFactory.GetFont("Times New Roman", 8, iTextSharp.text.Font.NORMAL, new BaseColor(System.Drawing.Color.Black));
        string titulo = "";

        public PdfHandlerEvents(string title)
        {
            titulo = title;
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            iTextSharp.text.Font font2 = iTextSharp.text.FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.BOLD, new BaseColor(System.Drawing.Color.Black));

            iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Properties.Resources.logo_transp, System.Drawing.Imaging.ImageFormat.Png);
            logo.Border = iTextSharp.text.Rectangle.BOX;
            logo.ScalePercent(50);
            logo.Alignment = iTextSharp.text.Image.LEFT_ALIGN;
            document.Add(logo);

            string cabecalho = "                        Ortodoctor                                               " + DateTime.Now.ToString("dd/MM/yyyy  HH:mm:ss") +"\n" +
                                 "Rua Almirante Barroso, 2976 - Toledo-PR";

            PdfContentByte cbo = writer.DirectContent;
            ColumnText colunaCabecalho = new ColumnText(cbo);
            colunaCabecalho.SetSimpleColumn(new Phrase(new Chunk(cabecalho, font2)),
                               210, 810, 580, 36, 10, Element.ALIGN_CENTER | Element.ALIGN_TOP);
            colunaCabecalho.Go();

            string linha =
                "______________________________________________________________________________________________\n" +
                 titulo;

            document.Add(new Paragraph(linha, font2));
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            PdfContentByte cb = writer.DirectContent;
            BaseFont font;
            font = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
            cb.SetFontAndSize(font, 9);
            string texto = "Página: " + writer.PageNumber.ToString();
            cb.ShowTextAligned(Element.ALIGN_RIGHT, texto, document.Right, document.Bottom - 20, 0);
        }
    }
}